package com.shanzmoo.merchant.manage.api.manager.product;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.util.CollectionUtil;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.module.product.domain.dto.AttributeSaveDto;
import com.shanzmoo.db.module.product.domain.dto.AttributeUpdateDto;
import com.shanzmoo.db.module.product.domain.query.AttributeQueryVO;
import com.shanzmoo.db.module.product.domain.vo.AttributeVO;
import com.shanzmoo.db.module.product.domain.vo.BrandVO;
import com.shanzmoo.db.module.product.entity.ProductAttributeEntity;
import com.shanzmoo.db.module.product.entity.ProductBrandEntity;
import com.shanzmoo.db.module.product.service.IProductAttributeService;
import com.shanzmoo.merchant.manage.api.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品规格manager
 * @author xueqz
 */
@Component
@RequiredArgsConstructor
public class ProductAttributeManager {

    @Autowired
    private IProductAttributeService productAttributeService;

    public SHttpResponse<PageRespBase<AttributeVO>> attributeList(AttributeQueryVO attributeQueryVO) {
        // 开启分页
        Page page = productAttributeService.page(attributeQueryVO.page(), new QueryWrapper<ProductAttributeEntity>()
                .eq("deleted", false));
        List<AttributeVO> reList = CollectionUtil.convert(page.getRecords(), AttributeVO.class);
        PageRespBase<AttributeVO> re = PageRespBase.create(page,reList);
        return SHttpResponse.ok(re);
    }

    public SHttpResponse<Boolean> addAttribute(AttributeSaveDto attributeSaveDto) {
        ProductAttributeEntity data = attributeSaveDto.convert(ProductAttributeEntity.class);
        data.setCreateUser(SecurityUtil.getCurrentUserId());
        Assert.isTrue(productAttributeService.save(data),"创建商品规格失败");
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> delAttributeById(Integer attributeId) {
        ProductAttributeEntity entity = new ProductAttributeEntity();
        entity.setId(attributeId);
        entity.setDeleted(Boolean.TRUE);
        entity.setUpdateUser(SecurityUtil.getCurrentUserId());
        Assert.isTrue(productAttributeService.updateById(entity),"更新商品规格失败");
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> updateAttribute(AttributeUpdateDto attributeUpdateDto) {
        ProductAttributeEntity convert = attributeUpdateDto.convert(ProductAttributeEntity.class);
        Assert.isTrue(productAttributeService.updateById(convert),"更新商品规格失败");
        return SHttpResponse.ok();
    }
}
